package com.zhiche.lisa.bms.service.utils.common;

import com.google.common.base.Strings;
import com.mysql.jdbc.StringUtils;
import com.zhiche.lisa.bms.pojo.vo.common.AttachDetailVO;
import com.zhiche.lisa.core.utils.qiniu.config.QiniuConfig;
import com.zhiche.lisa.core.utils.qiniu.util.QiniuUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: liuanshun
 * @Description:
 * @Date: Create in 10:09 2018/10/19
 */
@Component
public class UploadUtil {

	private static final Logger LOGGER = LoggerFactory.getLogger(UploadUtil.class);

	@Autowired
	private QiniuConfig qiniuConfig;

	/**
	 * 增加绑定文件数据
	 * 根据保留key增加绑定下载路径，查看路径上传，时间等
	 *
	 * @param attachDetailVO 包含key和其他附加信息的对象
	 * @return
	 */
	public AttachDetailVO addAttachUploadResult(AttachDetailVO attachDetailVO) {
		//下载的文件信息
		String fileName = attachDetailVO.getAttachName();
		String key = attachDetailVO.getAttachKey();
		String lookUrl = QiniuUtils.generateDownloadTicket(qiniuConfig.getDownloadUrl(), key);
		attachDetailVO.setLookUrl(lookUrl);
		try {
			attachDetailVO.setFormat(QiniuUtils.getType(key));
		} catch (Exception e) {
			LOGGER.error("获取文件格式异常...", e);
		}
		if (StringUtils.isNullOrEmpty(fileName)) {
			String type = attachDetailVO.getFormat();
			if (!Strings.isNullOrEmpty(type)) {
				String[] typeList = type.split("/");
				if (attachDetailVO.getAttachType() == null) {
					if (ArrayUtils.isNotEmpty(typeList) && typeList.length > 0)
						attachDetailVO.setAttachType(typeList[typeList.length - 1]);
				}
				if (attachDetailVO.getAttachType() != null) {
					fileName = key + "." + attachDetailVO.getAttachType();
				}
			}
		} else {
            String newFileName = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
            if (org.apache.commons.lang3.StringUtils.isNotBlank(newFileName)) {
                attachDetailVO.setAttachType(newFileName);
            }
		}
		String downloadUrl = lookUrl;
		// 下载地址
		if (org.apache.commons.lang3.StringUtils.isNotBlank(fileName)) {
			downloadUrl =  downloadUrl + "&attname=" + fileName;
		}
		attachDetailVO.setDownloadUrl(downloadUrl);

		try {
			attachDetailVO.setSize(QiniuUtils.getSize(key));
		} catch (Exception e) {
			LOGGER.error("获取文件大小异常...", e);
		}

		// 文件上传时间
		try {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Long time = QiniuUtils.getPutTime(key);
			if (time != null) {
				String timeStr = time.toString().substring(0, 10);
				attachDetailVO.setUploadTime(simpleDateFormat.format(new Date(Long.valueOf(timeStr + "000"))));
			}
		} catch (Exception e) {
			LOGGER.error("获取文件上传时间异常...", e);
		}
		attachDetailVO.setAttachName(fileName);
		return attachDetailVO;
	}

	/**
	 * 增加绑定文件数据
	 * 根据保留key增加绑定下载路径，查看路径上传，时间等
	 *
	 * @param attachDetailVO 包含key和其他附加信息的对象
	 * @return
	 */
	public AttachDetailVO addAttachUploadResultNew(AttachDetailVO attachDetailVO) {
		//下载的文件信息
		String fileName = "";
		String key = attachDetailVO.getAttachKey();
		String lookUrl = QiniuUtils.generateDownloadTicket(qiniuConfig.getDownloadUrl(), key);
		attachDetailVO.setLookUrl(lookUrl);
		try {
			attachDetailVO.setFormat(QiniuUtils.getType(key));
		} catch (Exception e) {
			LOGGER.error("获取文件格式异常...", e);
		}
		// 下载地址
		String downloadUrl = lookUrl + "&attname=" + attachDetailVO.getAttachName();
		attachDetailVO.setDownloadUrl(downloadUrl);

		try {
			attachDetailVO.setSize(QiniuUtils.getSize(key));
		} catch (Exception e) {
			LOGGER.error("获取文件大小异常...", e);
		}

		// 文件上传时间
		try {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Long time = QiniuUtils.getPutTime(key);
			if (time != null) {
				String timeStr = time.toString().substring(0, 10);
				attachDetailVO.setUploadTime(simpleDateFormat.format(new Date(Long.valueOf(timeStr + "000"))));
			}
		} catch (Exception e) {
			LOGGER.error("获取文件上传时间异常...", e);
		}
		return attachDetailVO;
	}
}
